/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Reinhard
 */
public class DB {

  private Connection connection;
  private Statement stat;
  private ResultSet result;

  public DB() {
    String db_url = "jdbc:mysql://localhost/8box_repository";
    String db_user = "progin";
    String db_pass = "progin";
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      connection = DriverManager.getConnection(db_url, db_user, db_pass);
    } catch (SQLException e) {
      System.out.println(e);
    } catch (InstantiationException e) {
      System.out.println(e);
    } catch (IllegalAccessException e) {
      System.out.println(e);
    } catch (ClassNotFoundException e) {
      System.out.println(e);
    }
  }

  public void executeQuery(String q) throws SQLException {
    stat = connection.createStatement();
    result = stat.executeQuery(q);
  }

  public void executeUpdate(String q) throws SQLException {
    stat = connection.createStatement();
    stat.executeUpdate(q);
  }

  public void execute(String q) throws SQLException {
    stat = connection.createStatement();
    if (stat.execute(q)) {
      result = stat.getResultSet();
    }
  }

  public ResultSet getResultSet() {
    return result;
  }
  
  public static void main(String[] args) throws SQLException{
    // Tes DB
    DB db = new DB();
    db.executeQuery("SELECT * from user");
    ResultSet rs = db.getResultSet();
    while(rs.next()){
      System.out.println(rs.getString("nama") + rs.getString("email"));
    }
  }
}
